(function() {
	$J('body').on('change', '#province', function() {
		initCities();
	});
	$J('body').on('change', '#city', function() {
		initDistricts();
	});
	$J('body').on('change', '#district', function() {
		initLandmarks();
	});
		
	initAreas($J('#province').data('v'), $J('#city').data('v'), $J('#district').data('v'));
})();

	// 初始化省市区县列表
	function initDistrictOnly(oldDistrictId) {
		Province.getData(function() {
			initDistricts(oldDistrictId);
		});
	}

	function initAreas(oldProvinceId, oldCityId, oldDistrictId, oldLandmark) {
		Province.getData(function() {
			if(oldProvinceId) {
				$J(Province.allProvince).each(function(i, pro){
					if(oldProvinceId == pro.id) {
						$J("#province").append("<option value='"  + pro.id + "' selected>" + pro.name + "</option>");
					} else {
						$J("#province").append("<option value='"  + pro.id + "'>" + pro.name + "</option>");
					}
				});
				initCities(oldCityId, oldDistrictId, oldLandmark);
			} else {
				$J(Province.allProvince).each(function(i, pro){
					$J("#province").append("<option value='"  + pro.id + "'>" + pro.name + "</option>");
				});
			}
		});
	}
	
	function initCities(oldCityId, oldDistrictId, oldLandmark) {
		if ($J('#city').length == 0) {
			return;
		}
		$J("#city").html('<option value=""></option>');
		$J("#district").html('<option value=""></option>');
		var provinceId = $J('#province').val();
		if(provinceId != '') {
			var citys = Province.searchCity(provinceId);
			if(oldCityId) {
				$J(citys).each(function(i, c){
					if(oldCityId == c.id) {
						$J("#city").append("<option value='"  + c.id + "' selected>" + c.name + "</option>");
					} else {
						$J("#city").append("<option value='"  + c.id + "'>" + c.name + "</option>");
					}
				});
				initDistricts(oldDistrictId, oldLandmark);
			} else {
				$J(citys).each(function(i, c){
					$J("#city").append("<option value='"  + c.id + "'>" + c.name + "</option>");
				});
			}
		}
	}
	
	function initDistricts(oldDistrictId, oldLandmark) {
		if ($J("#district").length == 0) {
			return;
		}
		$J("#district").html('<option value=""></option>');
		var cityId = $J('#city').val();
		if(cityId != '') {
			var districts = Province.searchDistrict(cityId);
			if(oldDistrictId && oldDistrictId > 0) {
				$J(districts).each(function(i, dis){
					if(oldDistrictId == dis.id) {
						$J("#district").append("<option value='"  + dis.id + "' selected>" + dis.name + "</option>");
					} else {
						$J("#district").append("<option value='"  + dis.id + "'>" + dis.name + "</option>");
					}
				});
				initLandmarks(oldLandmark);
			} else {
				$J(districts).each(function(i, dis){
					$J("#district").append("<option value='"  + dis.id + "'>" + dis.name + "</option>");
				});
			}
		}
	}
	
	var initLandmarks = function(oldLandmark) {
		if ($J("#landmark").length == 0) {
			return;
		}
		$J("#landmark").html('<option value=""></option>');
		var districtId = $J('#district').val();
		if(districtId != '') {
			ajaxGet(window.baseUrl + "/area/landmarks/" + $J("#district").val(), {}, function(landmarks) {
				if(oldLandmark && oldLandmark > 0) {
					$J.each(landmarks, function(i, l){
						if(oldLandmark == l.id) {
							$J("#landmark").append("<option value='"  + l.id + "' selected>" + l.name + "</option>");
						} else {
							$J("#landmark").append("<option value='"  + l.id + "'>" + l.name + "</option>");
						}
					});
				} else {
					$J.each(landmarks, function(i, l){
						$J("#landmark").append("<option value='"  + l.id + "'>" + l.name + "</option>");
					});
				}
			});
		}
	}